﻿using Ross.OA.Client.Dto;
using Ross.OA.Services;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ross.OA.Client
{
    public partial class FormReceivePO : Form
    {
        private string _skinFile;
        public List<ReceivePODto> dataSource;
        private CommDBService _service;
        public FormReceivePO(string skinFile)
        {
            InitializeComponent();
            _skinFile = skinFile;
            _service = new CommDBService();
        }

        private void FormReceivePO_Load(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = _skinFile;          
            //this.splitContainer1.SplitterDistance = 560;
            SetData();
        }
        private void SetData()
        {
            if (dataSource.Count > 0)
            {
                order_code.Text = dataSource[0].order_code;
                item_num_sum.Text = dataSource[0].item_num_sum.ToString();
                delivery_date.Text = dataSource[0].delivery_date;
                dataGridViewM.DataSource = dataSource[0].data;
                SetDGVM();
            }
        }

        private void SetDGVM()
        {
            dataGridViewM.Columns[0].Width = 50;
            dataGridViewM.Columns[0].HeaderText = "选择";            
            dataGridViewM.Columns[1].HeaderText = "采购行号";
            dataGridViewM.Columns[2].Width = 165;
            dataGridViewM.Columns[2].HeaderText = "物料编码";
            dataGridViewM.Columns[3].Width = 360;
            dataGridViewM.Columns[3].HeaderText = "物料名称";
            dataGridViewM.Columns[4].Width = 100;
            dataGridViewM.Columns[4].HeaderText = "采购数量";
            dataGridViewM.Columns[5].Width = 80;
            dataGridViewM.Columns[5].HeaderText = "已设置";
            dataGridViewM.Columns[5].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridViewM.Columns[5].DisplayIndex = 1;
            dataGridViewM.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridViewM.AllowUserToAddRows = false;
            dataGridViewM.ReadOnly = true;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            foreach (var item in dataSource)
            {
                if (item.data != null && item.data.Count > 0)
                {
                    var d = item.data.FirstOrDefault();
                    if (d.quantity_rate == null || d.quantity_rate.Length <= 0)
                    {
                        Comms.Warn("请填写下线合格率！");
                        return;
                    }
                    else if (d.quantity_rate[0].fits_rate <= 0)
                    {
                        Comms.Warn("下线合格率必须大于 0！");
                        return;
                    }
                    int chanliang = 0;
                    if (d.schedule != null && d.stock != null)
                    {
                        if (d.schedule.Length > 0 && d.stock.Length > 0)
                        {
                            chanliang = d.schedule[0].fits_schedule + d.stock[0].fits_stock;
                        }
                        else if (d.schedule.Length > 0 && d.stock.Length <= 0)
                        {
                            chanliang = d.schedule[0].fits_schedule;
                        }
                        else if (d.schedule.Length <= 0 && d.stock.Length > 0)
                        {
                            chanliang = d.stock[0].fits_stock;
                        }
                    }
                    if (chanliang < d.item_num)
                    {
                        Comms.Warn("供应商成品库存+排产数量不能小于采购数量!");
                        return;
                    }
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void DoSetData()
        {
            ReceivePODto dto = dataSource[0];
            if (dataGridViewM.SelectedRows.Count <= 0)
            {
                Comms.Warn("请至少选择一行！");
                return;
            }
            var detail = dataGridViewM.SelectedRows[0].DataBoundItem as ReceiveDetail;            
            //dto.data = new List<ReceiveDetail>();
            //ReceiveDetail detail = new ReceiveDetail();
            if(Comms.CInt(fits_stock.Text)+ Comms.CInt(fits_schedule.Text) < detail.item_num)
            {
                Comms.Warn("供应商成品库存+排产数量不能小于采购数量!");
                return;
            }
            if (Comms.CInt(fits_rate.Text) <= 0)
            {
                Comms.Warn("下线合格率必须大于 0！");
                return;
            }
            detail.stock = new Stock[] { new Stock() {
                    stock_date = stock_date.Value.ToString("yyyy-MM-dd"),
                    fits_stock = Comms.CInt(fits_stock.Text)
                } };
            detail.schedule = new Schedule[] { new Schedule() {
                    schedule_code = schedule_code.Text,
                    fits_schedule = Comms.CInt(fits_schedule.Text),
                    schedule_date = schedule_date.Value.ToString("yyyy-MM-dd")
                } };
            detail.quantity_rate = new Quantity_Rate_PO[] { new Quantity_Rate_PO() {
                    quantity_date = quantity_date.Value.ToString("yyyy-MM-dd"),
                    fits_quantity = Comms.CInt(fits_quantity.Text),
                    fits_rate = Comms.CInt(fits_rate.Text)
                } };
            detail.HaSet = "√";
            //dto.data.Add(detail);
            var resps = _service.SetPOReviceDetail(new EntityFramework.Model.POReciveDetail
            {
                order_code = dto.order_code,
                order_line = detail.order_line,
                stock_date = detail.stock[0].stock_date,
                fits_stock = detail.stock[0].fits_stock,
                schedule_code = detail.schedule[0].schedule_code,
                fits_schedule = detail.schedule[0].fits_schedule,
                schedule_date = detail.schedule[0].schedule_date,
                fits_quantity = detail.quantity_rate[0].fits_quantity,
                quantity_date = detail.quantity_rate[0].quantity_date,
                fits_rate = detail.quantity_rate[0].fits_rate
            });
            Comms.Info(resps == "ok" ? "设置成功！" : resps);
        }

        private void fits_stock_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;
            }
        }

        private void fits_schedule_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;
            }
        }

        private void fits_quantity_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;
            }
        }

        private void fits_rate_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;
            }
        }

        private void dataGridViewM_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewM.SelectedRows.Count > 0)
            {
                int selectedIndex = dataGridViewM.SelectedRows[0].Index;
                foreach (DataGridViewRow row in dataGridViewM.Rows)
                {
                    if (row.Cells[0].Value == null)
                        row.Cells[0].Value = false;
                    row.Cells[0].Value = (row.Index == selectedIndex);
                }

                var detail = dataGridViewM.SelectedRows[0].DataBoundItem as ReceiveDetail;
                order_line.Text = detail.order_line;
                fits_code.Text = detail.fits_code;
                fits_name.Text = detail.fits_name;
                item_num.Text = detail.item_num.ToString();
                if (detail.stock.Length > 0)
                {
                    stock_date.Value = Comms.CDate(detail.stock[0].stock_date).Value;
                    fits_stock.Text = detail.stock[0].fits_stock.ToString();
                }
                else
                {
                    fits_stock.Text = "";
                }
                if (detail.quantity_rate.Length > 0)
                {
                    fits_rate.Text = detail.quantity_rate[0].fits_rate.ToString();
                    fits_quantity.Text = detail.quantity_rate[0].fits_quantity.ToString();
                    quantity_date.Value = Comms.CDate(detail.quantity_rate[0].quantity_date).Value;
                }
                else
                {
                    fits_rate.Text = "";
                    fits_quantity.Text = "";
                }
                if (detail.schedule.Length > 0)
                {
                    schedule_code.Text = detail.schedule[0].schedule_code.ToString();
                    fits_schedule.Text = detail.schedule[0].fits_schedule.ToString();
                    schedule_date.Value = Comms.CDate(detail.schedule[0].schedule_date).Value;
                }
                else
                {
                    schedule_code.Text = "";
                    fits_schedule.Text = "";
                }
            }
        }

        private void buttonSet_Click(object sender, EventArgs e)
        {
            DoSetData();
        }
    }
}
